<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/extras/spring-security">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/lib/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="http://117.80.114.220:81/static/css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset" sec:authorize="hasAuthority('sys:user:query')">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <form class="layui-form layui-form-pane" lay-filter="data-search-form" action="">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">用户姓名</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userName" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">用户编码</label>
                            <div class="layui-input-inline">
                                <input type="text" name="userCode" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">手机</label>
                            <div class="layui-input-inline">
                                <input type="text" name="mobile" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">邮箱</label>
                            <div class="layui-input-inline">
                                <input type="text" name="email" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-clear-btn"><i class="layui-icon"></i> 重置</button>
                        </div>
                    </div>
                </form>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarUser" >
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add" sec:authorize="hasAuthority('sys:user:add')"> 添加 </button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete" sec:authorize="hasAuthority('sys:user:delete')"> 删除 </button>
            </div>
        </script>

        <table class="layui-hide" id="userTableId" lay-filter="userTableFilter"></table>

        <script type="text/html" id="userTableBar">
            <a class="layui-btn layui-btn-xs data-count-edit" lay-event="role" sec:authorize="hasAuthority('sys:user:role')">授权</a>
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit" sec:authorize="hasAuthority('sys:user:update')">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete" sec:authorize="hasAuthority('sys:user:delete')">删除</a>
        </script>

    </div>
</div>
<script src="http://117.80.114.220:81/static/lib/layui/layui.js" charset="utf-8"></script>
<script src="http://117.80.114.220:81/static/lib/jquery-3.4.1/jquery-3.4.1.min.js"></script>
<script src="http://117.80.114.220:81/static/js/my.js"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            baseURL = layui.baseUrl,
            table = layui.table;

        table.render({
            elem: '#userTableId',
            url: baseURL + '/sys-user/list',
            toolbar: '#toolbarUser',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                {type: "checkbox", width: 50},
                {field: 'id', width: 80, title: 'ID'},
                {field: 'userName', minWidth: 100, title: '用户名'},
                {field: 'userCode', minWidth: 100, title: '用户编码'},
                {field: 'email', minWidth: 100, title: '邮箱'},
                {field: 'mobile', minWidth: 100, title: '手机'},
                {field: 'status', minWidth: 100, title: '状态',templet:statusTemplet},
                {field: 'createdByName', minWidth: 100, title: '创建者'},
                {field: 'createdTime', minWidth: 200, title: '创建时间' },
                {field: 'updatedByName', minWidth: 100, title: '修改者'},
                {field: 'updatedTime', minWidth: 200, title: '修改时间'},
                {title: '操作', minWidth: 200, toolbar: '#userTableBar', align: "center"}
            ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line'
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('userTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: result
                }
            }, 'data');

            return false;
        });
        // 监听重置操作
        form.on('submit(data-clear-btn)', function (data) {
            form.val('data-search-form',{userName:"",userCode:"",mobile:"",email:""});
            //执行搜索重载
            table.reload('userTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    searchParams: {}
                }
            }, 'data');
            return false;
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(userTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'add.html',
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var searchParam = JSON.stringify(form.val('data-search-form'));
                var datas = table.checkStatus('userTableId'); //id 即为基础参数 id 对应的值
                if(datas.data.length > 0){
                    //询问框
                    layer.confirm('确认要删除选中数据？', {
                        btn: ['确认','取消'] //按钮
                    }, function(){
                        var ids = [];
                        $.each(datas.data,function(i,e){
                            ids.push(e.id);
                        });
                        var userIds = JSON.stringify({userIds:ids});
                        myPost("/sys-user/deleteByIds",userIds,function(data){
                            if(data.code == 0){
                                layer.msg('删除成功', {icon: 1});
                                //执行搜索重载
                                table.reload('userTableId', {
                                    page: {
                                        curr: 1
                                    }
                                    , where: {
                                        searchParams: searchParam
                                    }
                                }, 'data');
                            }else{
                                layer.msg(data.msg);
                            }
                        });

                    });
                }
            }
        });
        //监听表格复选框选择
        table.on('checkbox(userTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(userTableFilter)', function (obj) {
            var searchParam = JSON.stringify(form.val('data-search-form'));
            var data = obj.data;
            if (obj.event === 'edit') {
                var index = layer.open({
                    title: '编辑用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'edit.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.ajax({
                        url: baseURL+"/sys-user/delete",
                        type: "post",
                        data: {id:data.id},
                        success: function(data){
                            if(data.code == 0){
                                layer.msg('删除成功', {icon: 1});
                                //执行搜索重载
                                table.reload('userTableId');
                            }else{
                                layer.msg(data.msg);
                            }
                        },
                        error:function(data){
                            layer.msg(data.responseJSON.msg);
                        },
                        async:false,
                    });
                    layer.close(index);
                });
            } else if (obj.event === 'role') {
                var index = layer.open({
                    title: '用户授权',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'user_role.html?id='+data.id,
                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            }
        });

    });
</script>
</body>
</html>
